<?php

namespace Platform\Services\User;

use Platform\Services\BaseService;

class ReportService extends BaseService {

    public function addReport($userId, $objectId, $objectType, $content, $userInfo) {
        $userId = intval($userId) ? trim($userId) : E('用户ID不能为空');
        $objectId = intval($objectId) ? intval($objectId) : E('被举报的类型ID不能为空');
        $objectType = intval($objectType) ? intval($objectType) : E('被举报的类型不能为空');
        $content = trim($content) ? trim($content) : E('举报内容不能为空');
        $barrageReportModel = model("Report/Report");
        $isReport = $barrageReportModel->checkIsReportObject($userId, $objectId, $objectType);
        if ($isReport) {
            E("您已经举报过此弹幕");
        }
        switch ($objectType) {
            case REPORT_TYPE['REDPACKET']:
                $videoInfo = service("Video/VideoService")->getVideoInfoById($objectId);
                if (empty($videoInfo)) {
                    E("举报对象不存在");
                }
                $beReportUserInfo = service("User/UserService")->getUserInfoByUserId($videoInfo['userId']);
                $beReportUserName = $beReportUserInfo['nickname'];
                $beReportUserId = $videoInfo['userId'];
                break;
        }
        $barrageReportModel->setUserId($userId);
        $barrageReportModel->setObjectId($objectId);
        $barrageReportModel->setObjectType($objectType);
        $barrageReportModel->setBeReportUserId($beReportUserId);
        $barrageReportModel->setUserId($userId);
        $barrageReportModel->setUserName($userInfo['nickname']);
        $barrageReportModel->setCreateTime(time());
        $barrageReportModel->setIsDel(IS_DEL_NO);
        $barrageReportModel->setContent($content);
        $barrageReportModel->setHandleStatus(0);
        $result = $barrageReportModel->save();
        if ($result == false) {
            E("添加失败");
        }
        return $barrageReportModel->getId();
    }

    public function updateStatusByIds($ids, $status = ARTICLE_STATUS['CHECKED'], $adminId = 0) {
        if (!is_array($ids)) {
            $ids = explode(",", $ids);
        }
        if (empty($ids)) {
            E("审核ID不能为空");
        }
        $reportModel = model("Report/Report");
        $result = $reportModel->updateStatusByIds($ids, $status, $adminId);
        return $result;
    }

    /**
     * 获取其他相同object的记录
     * @param type $ids 
     */
    public function getSameObjectLog($ids) {
        $reportModel = model("Report/Report");
        $condition['id'] = array("in", $ids);
        $list = $reportModel->getLists($condition, array("id", "objectId", "objectType"), "", 0, 0);
        foreach ($list as $key => $value) {
            $objectArr[] = $value['objectId'];
        }
        unset($condition);
        $condition['objectType'] = $list[0]['objectType'];
        $condition['objectId'] = array("in", $objectArr);
        $condition['handleStatus'] = REPORT_STATUS['APPLY'];
        $list = $reportModel->getLists($condition, array("id", "objectId", "objectType", "userId"), "", 0, 0);
        foreach ($list as $key => $value) {
            $idArr[] = $value['id'];
            if (!in_array($value['objectId'], $objectIdArr)) {
                $objectIdArr[] = $value['objectId'];
            }
            $userIdArr[] = $value['userId'];
        }
        $result['ids'] = $idArr;
        $result['objectIds'] = $objectIdArr;
        $result['userIds'] = $userIdArr;
        return $result;
    }

}

?>
